<template>
  <div>
    <common-head :title="$route.name"/>
    <div class="container-cart">
      <!-- 我的收藏数据 -->
      <div v-if="isShow">
        <van-swipe-cell v-for="item in items" :key="item.id">
          <div class="item">
            <van-card
              :desc="item.dateAdd"
              :title="item.goodsName"
              :thumb="item.pic"
            >
            </van-card>
          </div>
            <template #right>
              <van-button square text="删除" type="danger" class="delete-button" @click="beforeClose(item.goodsId)"/>
            </template>
        </van-swipe-cell>
      </div>
      <!-- 我的收藏数据 -->
      <!-- 空状态 -->
      <div v-else class="empty-box">
        <van-empty
          class="custom-image"
          image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
          description="您没有任何收藏哟"
        />
      </div>
    </div>
  </div>
</template>

<script>
import CommonHead from '_components/CommonHead'
import { fetchFavorList, delFavorItem } from '_api'
import { getToken } from '../../utils/index'

export default {
  name: 'Cart',
  data () {
    return {
      items: [], // 购物车数据
      isShow: false
    }
  },
  created () {
    this.fetchFavorLists()
  },
  methods: {
    // 删除确认提示
    beforeClose (id) {
      this.$dialog.confirm({
        message: '确定删除吗？',
        theme: 'round-button'
      }).then(() => {
        delFavorItem({
          type: 0,
          goodsId: id,
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.$toast.success({
              message: '删除成功',
              duration: 1000,
              onClose: () => {
                this.fetchFavorLists()
              }
            })
          } else if (res.data.code === 700) {
            this.items = []
            this.isShow = false
          }
        })
      }).catch(() => {
        // on cancel
      })
    },
    // 请求收藏数据
    fetchFavorLists () {
      fetchFavorList({
        type: 0,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
          this.isShow = true
        }
        if (res.data.code === 700) {
          this.items = []
          this.isShow = false
        }
      })
    }
  },
  components: { CommonHead }
}
</script>

<style lang="scss" scoped>
.container-cart{
  padding-bottom:100px;
  .item{
    display: flex;
    background-color: #fff;
    .van-card{
      flex: 1;
    }
  }
  ::v-deep .van-swipe-cell__right{
    background-color: #fff;
  }
  .goods-card {
      margin: 0;
    }
  .delete-button{
    margin-top: 8px;
    height: 100%;
  }
}
.empty-box{
  .van-empty{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .custom-image .van-empty__image {
    width: 2.4324rem;
    height: 2.4324rem;
  }
  .van-card__title{
    font-size: 0.4rem;
  }
  .van-card__desc{
    margin-top: 0.3rem;
  }
}
</style>
